<?php
namespace App\Http\Controllers\Admin\Member;

use App\admin\FormView\UserInfoFormView;
use App\admin\TableView\ApilistView;
use App\admin\TableView\UserListView;
use App\Repositories\Admin\UserRepository;
use App\Utilities\JiugeForm;
use App\Utilities\Upload;
use Illuminate\Http\Request;
use Hash;

class UserController extends BaseController
{
    /**
     * 用户
     */

    protected $view = 'admin.member.user.';//视图前缀
    protected $url = 'user';//路由前缀
    protected $model;
//    protected $formElementArr = [//表单元素
//        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
//        array(1,'手机号码','phone','手机号码',2),
//        array(1,'用户名','name','用户名',1),
//        array(5,'用户类型','type','用户类型',6),
//    ];
//    protected $fieldShowArr = [
//        'id' => 'ID',
//        'phone' => '手机号码',
//        'name' => '用户名',
//        'type' => '用户类型',
//        'createTime' => '创建时间',
//    ];

    public function __construct()
    {
        parent::__construct();
        /*
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new UserRepository();
        view()->share('model',$this->model);
        view()->share('prefix_url',$this->prefix_url);*/
//        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index(Request $Request)
    {
        return (new UserListView($Request))
            ->get_html($this);
    }
    public function userinfo(){
        return (new UserInfoFormView())
            ->get_html($this);
    }

    public function create()
    {
        return view($this->view.'edit',array(
            'types' => $this->model->getTypeList(),
        ));
//        return view($this->formView.'edit',array(
//            'sels' => $this->formElementArr,
//            'options' => array(
//                'type' => $this->model->getTypeList(),
//            ),
//        ));
    }

    public function store(Request $request)
    {
        $model = $this->model->getOneByName();
        if ($model) {
            echo "<script>alert('已有同名称用户！');history.go(-1);</script>"; exit;
        }
        $data = $this->getData($request);
        $data['password'] = Hash::make('12345');
        $this->model->setAdd($data);
        return redirect($this->prefix_url);
    }

    public function edit($id)
    {
        return view($this->view.'edit',array(
            'types' => $this->model->getTypeList(),
            'data' => $this->getModelById($id),
        ));
//        return view($this->formView.'edit',array(
//            'sels' => $this->formElementArr,
//            'options' => array(),
//            'data' => $this->model->getOne($id),
//        ));
    }

    public function update(Request $request,$id)
    {
        $model = $this->model->getShowOne($id);
        $data = $this->getData($request);
        //头像图片处理
        if ($model->head && $data['head']) {
            $headOld = $model->head;
        }
        if (!$data['head']) { $data['head'] = $model->head; }
        //更新数据
        $this->model->setModify($data,$id);
        //去除老图片
        if (isset($headOld) && file_exists(ltrim($headOld,'/'))) {
            unlink(ltrim($headOld,'/'));
        }
        return redirect($this->prefix_url);
    }

    /**
     * 设置删除
     */
    public function setDel(Request $request)
    {
        $id = $request->id;
        $this->model->setDel($id);
        return redirect($this->prefix_url);
    }

    /**
     * 收集、验证
     */
    public function getData(Request $request)
    {
        $phone = $request->phone;
        $name = $request->name;
        $type = $request->type;
        $types = $this->model->getTypeList();
        $sex = $request->sex;
        $introducer = $request->introducer;
        if (!array_key_exists($type,$types)) {
            echo "<script>alert('用户类型错误！');history.go(-1);</script>"; exit;
        }
        if (isset($request->head)) {
            $resFile = Upload::uploadImg($request,'head');
            if ($resFile['code']!=0) {
                echo "<script>alert('上传错误！');history.go(-1);</script>"; exit;
            } else {
                $head = $resFile['filePath'];
            }
        } else {
            $head = '';
        }
        return array(
            'phone' =>$phone,
            'name' => $name,
            'type' => $type,
            'sex' => $sex,
            'head' => $head,
            'introducer' => $introducer,
        );
    }

    public function getModelById($id)
    {
        $model = $this->model->getShowOne($id);
        if (!$model) {
            echo "<script>alert('记录不存在！');history.go(-1);</script>"; exit;
        }
        return $model;
    }

    /**
     * 清空用户数据
     */
    public function setClean()
    {
        $resModel = $this->model->setClean();
        if ($resModel!=0) {
            echo "<script>";
            echo "alert('操作失败！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        return redirect($this->prefix_url);
    }
}